using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace RedBlueBalls
{
    /// <summary>
    /// An utility class for common methods
    /// </summary>
    public class Util
    {
        public const int RED_COUNT = 33;
        public const int BLUE_COUNT = 16;
        private static int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };

        /// <summary>
        /// Add a number to an integer list from big to small
        /// </summary>
        /// <param name="list"></param>
        /// <param name="number"></param>
        public static void Add(List<int> list, int number)
        {
            int count = list.Count;

            if (count == 0)
            {
                list.Add(number);
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    if (list[i] < number)
                    {
                        list.Insert(i, number);
                        return;
                    }
                }

                // The last one
                list.Add(number);
            }
        }

        /// <summary>
        /// Divide two numbers
        /// </summary>
        /// <param name="dividend"></param>
        /// <param name="divisor"></param>
        /// <returns></returns>
        public static double Divide(double dividend, double divisor)
        {
            if (divisor > 0)
            {
                return dividend / divisor;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// Get a string in percentage format
        /// </summary>
        /// <param name="numerator"></param>
        /// <param name="denominator"></param>
        /// <returns></returns>
        public static string GetPercentage(int numerator, int denominator)
        {
            if (denominator == 0)
                return "N/A";

            double percentage = Convert.ToDouble(numerator) / denominator * 100;
            return percentage.ToString("F2") + "%";
        }

        /// <summary>
        /// Get the maximum number in an array
        /// </summary>
        /// <param name="array"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static int GetMaximum(int[] array, int size)
        {
            int max = array[0];

            for (int i = 1; i < size; i++)
            {
                if (array[i] > max)
                    max = array[i];
            }

            return max;
        }

        /// <summary>
        /// Get the minimum number in an array
        /// </summary>
        /// <param name="array"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static int GetMinimum(int[] array, int size)
        {
            int min = array[0];

            for (int i = 1; i < size; i++)
            {
                if (array[i] < min)
                    min = array[i];
            }

            return min;
        }

        /// <summary>
        /// Check whether a number is prime number
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool IsPrime(int number)
        {
            foreach (int prime in primes)
            {
                if (number == prime)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Sort the array ascending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="size"></param>
        public static void SortAsc(int[] array, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (array[i] > array[j])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight ascending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortAsc(int[] array, int[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] > weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight ascending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortAsc(int[] array, long[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] > weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight ascending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortAsc(int[] array, double[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] > weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight descending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortDesc(int[] array, int[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] < weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight descending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortDesc(int[] array, long[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] < weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// Sort the array by weight descending
        /// </summary>
        /// <param name="array"></param>
        /// <param name="weight"></param>
        /// <param name="size"></param>
        public static void SortDesc(int[] array, double[] weight, int size)
        {
            int temp;
            for (int i = 0; i < size - 1; i++)
            {
                for (int j = i + 1; j < size; j++)
                {
                    if (weight[array[i]] < weight[array[j]])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
    }
}
